<template>
  <div class="payment-container">
    <div class="header">
      <div class="back-icon">
        <i class="fas fa-angle-left"></i>
      </div>
      <div class="title">支付收银台</div>
    </div>
    <div class="store-info">
      <div class="store-name">{{ storeName }}</div>
      <div class="price">¥{{ price }}</div>
      <div class="time-left">剩余支付时间: {{ timeLeft }}</div>
    </div>
    
    <div class="divider"></div>
    
    <div class="payment-methods">
      <div class="method-title">支付方式</div>
      <div class="method" @click="selectPaymentMethod('wechat')" :class="{selected: selectedMethod === 'wechat'}">
        <div class="method-left">
          <img src="/static/微信支付.png" alt="微信支付" class="icon" />
          <span>微信支付</span>
        </div>
        <div class="method-right">
          <div class="radio">
            <div class="radio-inner" v-if="selectedMethod === 'wechat'"></div>
          </div>
        </div>
      </div>
      <div class="method" @click="selectPaymentMethod('unionpay')" :class="{selected: selectedMethod === 'unionpay'}">
        <div class="method-left">
          <img src="/static/银联-支付.png" alt="银联支付" class="icon" />
          <span>银联支付</span>
        </div>
        <div class="method-right">
          <div class="radio">
            <div class="radio-inner" v-if="selectedMethod === 'unionpay'"></div>
          </div>
        </div>
      </div>
    </div>
    
    <div class="pay-button-container">
      <button class="pay-button" @click="handlePay">
        立即支付 ¥{{ price }}
      </button>
    </div>
  </div>
</template>

<script>
import config from "@/config.js";

export default {
  data() {
    return {
      selectedMethod: '', 
      price: 0,
      storeName: '',
      timeLeft: '30:00',
      timer: null,
      orderData: null 
    };
  },
  onLoad(options) {
    this.price = parseFloat(options.price);
    this.storeName = decodeURIComponent(options.storeName);
    this.orderData = JSON.parse(decodeURIComponent(options.orderData));
    this.startCountdown();
  },
  methods: {
    selectPaymentMethod(method) {
      this.selectedMethod = method;
    },
    startCountdown() {
      let minutes = 30;
      let seconds = 0;
      this.timer = setInterval(() => {
        if (seconds === 0) {
          if (minutes === 0) {
            clearInterval(this.timer);
            uni.showToast({
              title: '支付超时',
              icon: 'none'
            });
            uni.navigateBack();
            return;
          }
          minutes--;
          seconds = 59;
        } else {
          seconds--;
        }
        this.timeLeft = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
      }, 1000);
    },
    handlePay() {
      if (!this.selectedMethod) {
        uni.showToast({
          title: '请选择支付方式',
          icon: 'none'
        });
        return;
      }
      if (!this.orderData) {
        console.error('订单数据未获取到');
        return;
      }
      uni.request({
        url: `${config.api_host}dingdan`,
        method: 'POST',
        data: this.orderData,
        success: (res) => {
          console.log('订单提交成功:', res.data);
          const orderId = res.data._id;
          uni.switchTab({
            url: '/pages/dingdan/dingdan',
            success: () => {
              console.log('跳转成功');
            },
            fail: (err) => {
              console.error('跳转失败:', err);
            }
          });
        },
        fail: (error) => {
          console.error('订单提交失败:', error);
          uni.showToast({
            title: '订单提交失败',
            icon: 'none'
          });
        }
      });
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
};
</script>

<style scoped>
.payment-container {
  width: 100%;
  height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

.header {
  background-color: #fff;
  height: 44px;
  display: flex;
  align-items: center;
  padding: 0 15px;
  border-bottom: 1px solid #eee;
}

.back-icon {
  margin-right: 10px;
}

.title {
  font-size: 17px;
  font-weight: 500;
  color: #333;
  flex: 1;
  text-align: center;
}

.store-info {
  background-color: #fff;
  padding: 20px 15px;
  text-align: center;
  margin-bottom: 10px;
}

.store-name {
  font-size: 16px;
  color: #333;
  margin-bottom: 15px;
}

.price {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
}

.time-left {
  font-size: 14px;
  color: #999;
}

.divider {
  height: 10px;
  background-color: #f5f5f5;
}

.payment-methods {
  background-color: #fff;
  padding: 0 15px;
}

.method-title {
  padding: 15px 0;
  font-size: 15px;
  color: #999;
  border-bottom: 1px solid #eee;
}

.method {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.method-left {
  display: flex;
  align-items: center;
}

.method.selected {
  background-color: #f9f9f9;
}

.icon {
  width: 24px;
  height: 24px;
  margin-right: 10px;
}

.radio {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radio-inner {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: #07C160;
}

.pay-button-container {
  padding: 20px 15px;
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

.pay-button {
  width: 100%;
  height: 44px;
  background-color: #07C160;
  color: white;
  border: none;
  border-radius: 22px;
  font-size: 16px;
  font-weight: 500;
}
</style>